﻿var initialLocation;
var indonesia = new google.maps.LatLng(0, 118.07928126464844);
var map;
var markersArray = [];


function initGoogleMap(circuitId) {
    initialLocation = indonesia;

    var myOptions = {
        zoom: 4,
        maxZoom: 8,
        center: indonesia,
        scrollwheel: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);



    getLocationData(circuitId);
}

function getLocationData(circuitId) {
    var url = "Circuit/GetById";
    $.ajax({
        type: "GET",
        url: url,
        cache: false,
        dataType: 'jsonp',
        data: {circuitId},
        jsonCallback: 'Test',
        success: function (data) {
            if (data != null) {
                if (data.length > 0) {
                    for (i = 0; i < data.length; i++) {

                        setMarker(data[i]);
                    }
                }
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(thrownError);
        }
    });
}

function setMarker(data) {
    var lat = parseFloat(data.Latitude);
    var lon = parseFloat(data.Longitude);
    var markerLoc = new google.maps.LatLng(lat, lon);


    var marker = new google.maps.Marker({
        map: map,
        position: markerLoc,
        animation: "bounce"
    });

    markersArray.push(marker);
    marker.setMap(map);

}


function clearOverlays() {
    if (markersArray) {
        for (var i = 0; i < markersArray.length; i++) {
            markersArray[i].setMap(null);
        }
    }
    markersArray = new Array();
}